import request from './request'
import type { ChartData, DashboardData } from '@/stores'

// 仪表板相关API
export const dashboardApi = {
  // 获取仪表板数据
  getDashboardData(): Promise<DashboardData> {
    return request.get('/dashboard/data')
  },
  
  // 获取统计概览
  getStatistics(): Promise<any> {
    return request.get('/dashboard/statistics')
  }
}

// 图表相关API
export const chartApi = {
  // 获取图表列表
  getChartList(): Promise<ChartData[]> {
    return request.get('/charts')
  },
  
  // 获取图表详情
  getChartDetail(id: string): Promise<ChartData> {
    return request.get(`/charts/${id}`)
  },
  
  // 创建图表
  createChart(data: Partial<ChartData>): Promise<ChartData> {
    return request.post('/charts', data)
  },
  
  // 更新图表
  updateChart(id: string, data: Partial<ChartData>): Promise<ChartData> {
    return request.put(`/charts/${id}`, data)
  },
  
  // 删除图表
  deleteChart(id: string): Promise<void> {
    return request.delete(`/charts/${id}`)
  }
}

// 数据相关API
export const dataApi = {
  // 获取销售数据
  getSalesData(params?: any): Promise<any[]> {
    return request.get('/data/sales', { params })
  },
  
  // 获取用户数据
  getUserData(params?: any): Promise<any[]> {
    return request.get('/data/users', { params })
  },
  
  // 获取订单数据
  getOrderData(params?: any): Promise<any[]> {
    return request.get('/data/orders', { params })
  },
  
  // 导出数据
  exportData(type: string, params?: any): Promise<Blob> {
    return request.get(`/data/export/${type}`, { 
      params,
      responseType: 'blob'
    })
  }
}

// 模拟数据（用于开发测试）
export const mockApi = {
  // 模拟仪表板数据
  getDashboardData(): Promise<DashboardData> {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          totalSales: 1256789,
          totalUsers: 8967,
          totalOrders: 3456,
          growthRate: 12.5,
          recentCharts: [
            {
              id: '1',
              title: '销售趋势',
              type: 'line',
              data: [
                { name: '1月', value: 120 },
                { name: '2月', value: 132 },
                { name: '3月', value: 101 },
                { name: '4月', value: 134 },
                { name: '5月', value: 90 },
                { name: '6月', value: 230 }
              ]
            }
          ]
        })
      }, 500)
    })
  },
  
  // 模拟图表数据
  getChartList(): Promise<ChartData[]> {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve([
          {
            id: '1',
            title: '销售趋势图',
            type: 'line',
            data: [
              { name: '1月', value: 120 },
              { name: '2月', value: 132 },
              { name: '3月', value: 101 },
              { name: '4月', value: 134 },
              { name: '5月', value: 90 },
              { name: '6月', value: 230 }
            ]
          },
          {
            id: '2',
            title: '产品销量分布',
            type: 'pie',
            data: [
              { name: '产品A', value: 335 },
              { name: '产品B', value: 310 },
              { name: '产品C', value: 234 },
              { name: '产品D', value: 135 },
              { name: '产品E', value: 1548 }
            ]
          },
          {
            id: '3',
            title: '月度对比',
            type: 'bar',
            data: [
              { name: '上月', value: 820 },
              { name: '本月', value: 932 }
            ]
          }
        ])
      }, 300)
    })
  }
}